package com.yanqu.road.server.cmd.giftKey;

import com.yanqu.road.entity.giftKey.UserGiftKeyRedPacket;
import com.yanqu.road.pb.giftKey.GiftKeyProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.giftKey.GiftKeyModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

/**
 * @Author wpj
 * @Date 2021/12/31
 */
@Cmd(code = Protocol.S_GIFT_KEY_SLIDE_RED_PACKET, desc = "滑红包")
public class SlideGiftKeyRedPacketCmd extends AbstractCommand {

    GiftKeyProto.SlideRedPacketReqMsg msg;

    boolean end = false; // 是否结束 超出时间请求也算结束

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        msg = GiftKeyProto.SlideRedPacketReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        UserGiftKeyRedPacket userGiftKeyRedPacket = player.getModule(GiftKeyModule.class).getUserGiftKeyRedPacketMap().get(msg.getKey());

        // 是否存在
        if (userGiftKeyRedPacket == null) {
            log.error("{} not this red packet key : {}", player.getUserId(), msg.getKey());
            return;
        }

        // 是否已领奖
        if (userGiftKeyRedPacket.isGetIngots()) {
            log.error("{} has get ingots : {}", player.getUserId(), msg.getKey());
            return;
        }



        // 3,15,0.1,600,1|200,5,10|500,2,6     服务端只需要 |分割的第一个参数     倒计时(秒),游戏时长(秒),滑动间隔(秒客户端用),单次游戏最高获得的元宝数,..
        List<String> paramList = StringUtils.stringToStringList(StringUtils.stringToStringList(userGiftKeyRedPacket.getSettings(), "\\|").get(0), ",");

        long nowTime = System.currentTimeMillis();

        if (!msg.getEnd() && (msg.getReqTime() - userGiftKeyRedPacket.getLastReqTime()) < 500) {
            // 是否请求频繁
            log.error("{} req frequently : {} {} {}", player.getUserId(), msg.getKey(), msg.getReqTime(), userGiftKeyRedPacket.getLastReqTime());
            return;
        }


        // 只有 1 - 100的元宝被允许添加 并且最多只计算15次
        int size = msg.getOp().getIngotsList().size() >= 15 ? 15 : msg.getOp().getIngotsList().size();
        for (int i = 0; i < size; i++) {
            int ingots = msg.getOp().getIngotsList().get(i);
            if (ingots < 0 || ingots > 100) {
                continue;
            }
            userGiftKeyRedPacket.getIngotsList().add(ingots);
            userGiftKeyRedPacket.setUpdateOption();
        }

        if (msg.getEnd()) {
            player.getModule(GiftKeyModule.class).getRedPacketReward(userGiftKeyRedPacket);
            end = true;
        } else {

            // 判断是否超出游戏结束时间
            int gameTime = (Integer.valueOf(paramList.get(0)) + Integer.valueOf(paramList.get(1))) * 1000;
            if (userGiftKeyRedPacket.getStartTime() + gameTime < nowTime) {
                log.error("{} over time : {}", player.getUserId(), msg.getKey());
                player.getModule(GiftKeyModule.class).getRedPacketReward(userGiftKeyRedPacket);
                end = true;
                return;
            }

            userGiftKeyRedPacket.setLastReqTime(msg.getReqTime());
            userGiftKeyRedPacket.setUpdateOption();
        }

    }

    @Override
    public void sendData(GamePlayer player) {
        if (end) {
            GiftKeyProto.SlideRedPacketRespMsg.Builder resp = GiftKeyProto.SlideRedPacketRespMsg.newBuilder();
            resp.setIngots(player.getModule(GiftKeyModule.class).getUserGiftKeyRedPacketMap().get(msg.getKey()).getRealGetIngots());
            player.sendPacket(Protocol.U_GIFT_KEY_SLIDE_RED_PACKET, resp);
            log.debug("userId : {}, key : {}, ingots : {}", player.getUserId(), msg.getKey(), resp.getIngots());
        }
    }
}
